package org.spindle.soasuite.controllers;

import org.hsqldb.jdbc.JDBCDataSource;
import org.spindle.soasuite.core.api.AExecutable;
import org.spindle.soasuite.core.api.annotations.Executable;
import org.spindle.soasuite.core.api.annotations.Parameter;
import org.spindle.soasuite.core.constants.StateConstants;
import org.spindle.soasuite.data.internal.JDBCConfiguration;
import org.spindle.soasuite.data.internal.User;
import org.spindle.soasuite.datasource.external.DatasourceManager;

import java.util.ArrayList;
import java.util.List;

@Executable(path = "/datasource")
class DatasourceController extends AExecutable {

    @Parameter(name = "d")
    private String driver;

    @Parameter(name = "url")
    private String url;

    @Parameter(name = "cn")
    private String connectionName;

    @Parameter(name = "u")
    private String username;

    @Parameter(name = "p")
    private String password;

    @Parameter(name = "q")
    private String query;

    @Parameter(name = "m")
    private String method;

    @Parameter(name = "ds")
    private String datasource;


    private DatasourceManager ds = new DatasourceManager();

    @Override
    public Object execute() {

        User user = (User) getStateObject(StateConstants.USER);
        Object returnedObject = null;
        if ("mysql".equals(driver)) {
            driver = "com.mysql.jdbc.Driver";
        }

        if ("test".equals(method)) {
            try {
                returnedObject = ds.execute(driver, url, username, password, query);
            } catch (Exception e) {
                e.printStackTrace();
                returnedObject = e.getMessage();
            }
        } else if ("save".equals(method)) {
            JDBCConfiguration configuration = new JDBCConfiguration();
            configuration.setName(connectionName);
            configuration.setDriver(driver);
            configuration.setUrl(url);
            configuration.setUsername(username);
            configuration.setPassword(password);
            configuration.setUser(user);

            user.getDatasources().add(configuration);

            getInternalDatasource().persist(configuration);
        } else if ("list".equals(method)) {
            List<String> names = new ArrayList<String>();
            for (JDBCConfiguration ds : user.getDatasources()) {
                names.add(ds.getName());
            }
            returnedObject = names;
        } else if ("details".equals(method)) {
            for (JDBCConfiguration userDatasource : user.getDatasources()) {
                if (userDatasource.getName().equals(datasource)) {
                    returnedObject = userDatasource.getMappedConfiguration();
                    break;
                }
            }
        } else if ("delete".equals(method)) {

            //getInternalDatasource().refresh(user);
            for (JDBCConfiguration userDatasource : user.getDatasources()) {
                if (userDatasource.getName().equals(datasource)) {
                    getInternalDatasource().delete(userDatasource);
                    user.getDatasources().remove(userDatasource);
                    break;
                }
            }
        }

        return returnedObject;
    }

    @Override
    public String getName() {
        return "Datasource controller";
    }
}
